package com.irdstudio.efp.esb.api.rest.yed;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.bean.yed.req.NlsCreditOrApplOnWayReqBean;
import com.irdstudio.efp.esb.api.bean.yed.resp.NlsCreditOrApplOnWayRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.service.yed.NlsYedProcessService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 需求编号:
 * 问题编号:
 * 开发人员: zhangxc
 * 创建日期:2020年7月28日14:12:49
 * 功能描述:
 * 修改日期:2020年7月28日14:12:54
 * 修改描述:
 */
@RestController
@RequestMapping("/esb")
public class NlsCreditOrApplOnWayController implements EsbController {

    @PostMapping("/SERVICE_30230003_24")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.yed.class}) @ModelAttribute("reqCreditBean") NlsCreditOrApplOnWayReqBean reqWsdCreditBean, BindingResult bindingResult2, Exception exception){
        try {
            logger.info("======>优e贷查询在途授信或用信接口【"+reqWsdCreditBean.getRequestId() +"_30230003_24】开始<======");
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, reqWsdCreditBean, bindingResult2, exception,logger);
            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);

            NlsCreditOrApplOnWayRespBean resp = new NlsCreditOrApplOnWayRespBean();

            int count1=0;
            int count2=0;
            String isFlag = "N";//是否有在途授用信,  Y是，N否
            if ("1".equals(reqWsdCreditBean.getQryType())) {
                NlsCreditInfoVO nlsCreditInfoVO = new NlsCreditInfoVO();
                nlsCreditInfoVO.setCertCode(reqWsdCreditBean.getCertCode());
                nlsCreditInfoVO.setCertType(reqWsdCreditBean.getCertType());
                nlsCreditInfoVO.setLmtApplySeq(reqWsdCreditBean.getApplySeq());
                nlsCreditInfoVO.setPrdCode(reqWsdCreditBean.getPrdCode());
                count1 = nlsCreditInfoService.queryCreditOnWay(nlsCreditInfoVO);

            } else if ("2".equals(reqWsdCreditBean.getQryType())) {
                NlsApplyInfoVO nlsApplyInfoVO = new NlsApplyInfoVO();
                nlsApplyInfoVO.setCertCode(reqWsdCreditBean.getCertCode());
                nlsApplyInfoVO.setCertType(reqWsdCreditBean.getCertType());
                nlsApplyInfoVO.setApplySeq(reqWsdCreditBean.getApplySeq());
                nlsApplyInfoVO.setPrdCode(reqWsdCreditBean.getPrdCode());
                count2 = nlsApplyInfoService.countApplyInfoOnWay(nlsApplyInfoVO);
            }

            if(count1>0 || count2>0)
               isFlag = "Y";

            // 查询并返回
            return nlsYedProcessService.processYedCreditApply(reqWsdCreditBean,esbReqServiceBean,isFlag);

        }catch (ESBException e){
            NlsCreditOrApplOnWayRespBean failureRespBean = nlsYedProcessService.getFailureRespBean( e.getErrorCode(), e.getMessage());
            // 设置返回信息
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, failureRespBean, e.getErrorCode(), e.getMessage());
            return esbRespServiceBean;
        }finally {
            logger.info("======>优e贷查询在途授信或用信接口【"+reqWsdCreditBean.getRequestId() +"_30230003_24】结束<======");
        }
    }

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }
    @ModelAttribute("reqCreditBean")
    public NlsCreditOrApplOnWayReqBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, NlsCreditOrApplOnWayReqBean.class);
    }
    /** 日志信息*/
    private final Logger logger = LoggerFactory.getLogger(NlsCreditOrApplOnWayController.class);

    @Autowired
    @Qualifier("nlsCreditInfoService")
    private NlsCreditInfoService nlsCreditInfoService;

    @Autowired
    @Qualifier("nlsApplyInfoService")
    private NlsApplyInfoService nlsApplyInfoService;

    @Autowired
    @Qualifier("nlsYedProcessService")
    private NlsYedProcessService nlsYedProcessService;

}
